package cn.iocoder.yudao.module.alipay.controller.admin.cpsactivity;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.alipay.controller.admin.cpsactivity.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.cpsactivity.CpsActivityDO;
import cn.iocoder.yudao.module.alipay.convert.cpsactivity.CpsActivityConvert;
import cn.iocoder.yudao.module.alipay.service.cpsactivity.CpsActivityService;

@Tag(name = "管理后台 - cps活动")
@RestController
@RequestMapping("/alipay/cps-activity")
@Validated
public class CpsActivityController {

    @Resource
    private CpsActivityService cpsActivityService;

    @PostMapping("/create")
    @Operation(summary = "创建cps活动")
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity:create')")
    public CommonResult<Long> createCpsActivity(@Valid @RequestBody CpsActivityCreateReqVO createReqVO) {
        return success(cpsActivityService.createCpsActivity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新cps活动")
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity:update')")
    public CommonResult<Boolean> updateCpsActivity(@Valid @RequestBody CpsActivityUpdateReqVO updateReqVO) {
        cpsActivityService.updateCpsActivity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除cps活动")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity:delete')")
    public CommonResult<Boolean> deleteCpsActivity(@RequestParam("id") Long id) {
        cpsActivityService.deleteCpsActivity(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得cps活动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity:query')")
    public CommonResult<CpsActivityRespVO> getCpsActivity(@RequestParam("id") Long id) {
        CpsActivityDO cpsActivity = cpsActivityService.getCpsActivity(id);
        return success(CpsActivityConvert.INSTANCE.convert(cpsActivity));
    }

    @GetMapping("/list")
    @Operation(summary = "获得cps活动列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity:query')")
    public CommonResult<List<CpsActivityRespVO>> getCpsActivityList(@RequestParam("ids") Collection<Long> ids) {
        List<CpsActivityDO> list = cpsActivityService.getCpsActivityList(ids);
        return success(CpsActivityConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得cps活动分页")
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity:query')")
    public CommonResult<PageResult<CpsActivityRespVO>> getCpsActivityPage(@Valid CpsActivityPageReqVO pageVO) {
        PageResult<CpsActivityDO> pageResult = cpsActivityService.getCpsActivityPage(pageVO);
        return success(CpsActivityConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出cps活动 Excel")
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity:export')")
    @OperateLog(type = EXPORT)
    public void exportCpsActivityExcel(@Valid CpsActivityExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CpsActivityDO> list = cpsActivityService.getCpsActivityList(exportReqVO);
        // 导出 Excel
        List<CpsActivityExcelVO> datas = CpsActivityConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "cps活动.xls", "数据", CpsActivityExcelVO.class, datas);
    }

    @GetMapping("/getGenerate")
    @Operation(summary = "取链")
    @PreAuthorize("@ss.hasPermission('alipay:cps-activity:query')")
    public CommonResult<Object> getGenerate(@Valid CpsActivityExportReqVO pageVO) {
        return success(cpsActivityService.getGenerate(pageVO));
    }

}
